# From Greblus: https://blog.greblus.net/2014/11/18/msys2mingw-w64-cython-i-ipython-notebook/

_realname=zeromq
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.4
_cppzmqver=4.7.1
pkgrel=1
pkgdesc="Fast messaging system built on sockets, C and C++ bindings. aka 0MQ, ZMQ (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.zeromq.org/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-libsodium")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=("https://github.com/zeromq/libzmq/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "zmq-${_cppzmqver}.hpp::https://raw.githubusercontent.com/zeromq/cppzmq/v${_cppzmqver}/zmq.hpp"
        "zmq_addon-${_cppzmqver}.hpp::https://raw.githubusercontent.com/zeromq/cppzmq/v${_cppzmqver}/zmq_addon.hpp"
        "001-testutil_different_signedness-fix.patch"
        "002-unused.patch::https://patch-diff.githubusercontent.com/raw/zeromq/libzmq/pull/4181.patch")
sha256sums=('c593001a89f5a85dd2ddf564805deb860e02471171b3f204944857336295c3e5'
            '92b92acc0a93f9511097df20cc9ed4041bde4d0d9c6cdd6cc2ada29c84487ab8'
            '705bac35610437f22243da8cf300488d1e37c0815c60f70809051a8be8d0cb4c'
            '0bfdbd8caca39791c909c2cbeba5878a9c430d8a9dd1f06458f42656f593e8c6'
            '035e2d7cfa8a5d5fff2d0ae9be17c58f0e4d300cc34182091fa25d2d7ac4ac57')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-testutil_different_signedness-fix.patch \
    002-unused.patch

  autoreconf -fi
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH}
  cd ${srcdir}/build-${CARCH}

  # CXXFLAGS+=" -DZMQ_HAVE_WINDOWS_TARGET_XP" - for next version
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-libsodium \
    --enable-shared \
    --enable-static

  make
}

package() {
  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/include"

  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/zmq-${_cppzmqver}.hpp" "${pkgdir}${MINGW_PREFIX}/include/zmq.hpp"
  install -Dm644 "${srcdir}/zmq_addon-${_cppzmqver}.hpp" "${pkgdir}${MINGW_PREFIX}/include/zmq_addon.hpp"
  
  # Fix .pc file
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libzmq.pc"
}
